package com.longcai.piaojia.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.MineCouponListGet;
import com.longcai.piaojia.fragment.CouponExpiredFragment;
import com.longcai.piaojia.fragment.CouponNotUsedFragment;
import com.longcai.piaojia.fragment.CouponUsedFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by Administrator on 2016/8/2.
 */
public class MyCouponActivity extends BaseActivity implements View.OnClickListener {


    @BoundView(R.id.coupon_notused_tv) private TextView coupon_notused_tv;
    @BoundView(R.id.coupon_expired_tv) private TextView coupon_expired_tv;
    @BoundView(R.id.coupon_used_tv) private TextView coupon_used_tv;

    private CouponNotUsedFragment couponNotUsedFragment;
    private CouponExpiredFragment couponExpiredFragment;
    private CouponUsedFragment couponUsedFragment;

    private Fragment[] fragments;
    private int prePosition = 0;
    private int index;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_coupon);

        setBackTrue();

        setTitleName(getString(R.string.coupon_title_text));

        coupon_notused_tv.setOnClickListener(this);
        coupon_expired_tv.setOnClickListener(this);
        coupon_used_tv.setOnClickListener(this);

        couponNotUsedFragment = new CouponNotUsedFragment();
        couponExpiredFragment = new CouponExpiredFragment();
        couponUsedFragment = new CouponUsedFragment();

        fragments = new Fragment[]{couponNotUsedFragment, couponExpiredFragment, couponUsedFragment};

        getSupportFragmentManager().beginTransaction().add(R.id.coupon_container_rl, couponNotUsedFragment)
                .show(couponNotUsedFragment).commit();

    }

    @Override
    public void onClick(View view) {


        FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

        switch (view.getId()) {

            case R.id.coupon_notused_tv:

                index = 0;

                if (index != prePosition) {

//                    UtilLog.e("---------------->", "prePosition" + prePosition + "   index" + index);
                    initColor();
                    coupon_notused_tv.setTextColor(getResources().getColor(R.color.theme_color));

                    trx.hide(fragments[prePosition]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.coupon_container_rl, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    prePosition = index;
                }

                break;

            case R.id.coupon_expired_tv:

                index = 1;
                if (prePosition != index) {

                    initColor();

                    coupon_expired_tv.setTextColor(getResources().getColor(R.color.theme_color));

                    trx.hide(fragments[prePosition]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.coupon_container_rl, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    prePosition = index;
                }

                break;

            case R.id.coupon_used_tv:

                index = 2;
                if (prePosition != index) {

                    initColor();
                    coupon_used_tv.setTextColor(getResources().getColor(R.color.theme_color));

                    trx.hide(fragments[prePosition]);
                    if (!fragments[index].isAdded()) {
                        trx.add(R.id.coupon_container_rl, fragments[index]);
                    }
                    trx.show(fragments[index]).commit();

                    prePosition = index;
                }

                break;
        }

    }

    private void initColor() {

        coupon_notused_tv.setTextColor(getResources().getColor(R.color.text_color));
        coupon_expired_tv.setTextColor(getResources().getColor(R.color.text_color));
        coupon_used_tv.setTextColor(getResources().getColor(R.color.text_color));

    }
}
